package com.dayi.utils.pop;

import android.app.Activity;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dayi.R;
import com.lib.fastkit.utils.log.LogUtil;
import com.zyyoona7.popup.EasyPopup;

import androidx.cardview.widget.CardView;

public class PayStatePopupUtils {

    private Activity activity;

    public PayStatePopupUtils(Activity ac) {

        this.activity = ac;

        initPopuPopu();
    }

    private EasyPopup popu;

    private void initPopuPopu() {
        popu = EasyPopup.create()
                .setContext(activity)
                .setContentView(R.layout.pop_paystate)

                .setWidth(WindowManager.LayoutParams.FILL_PARENT)

                .setHeight(WindowManager.LayoutParams.FILL_PARENT)

                .setOnViewListener(new EasyPopup.OnViewListener() {
                    @Override
                    public void initViews(View view, EasyPopup basePopup) {

                        initView(view);


                    }


                })
                .setFocusAndOutsideEnable(true)
                .apply();

    }


    private LinearLayout lin_parent, lin_remind;
    private CardView card;
    private TextView tv_state, tv_content;
    private Button btn_sure, btn_cancel;


    private void initView(View view) {

        lin_parent = view.findViewById(R.id.lin_parent);
        card = view.findViewById(R.id.card);
        lin_remind = view.findViewById(R.id.lin_remind);

        tv_state = view.findViewById(R.id.tv_state);
        tv_content = view.findViewById(R.id.tv_content);

        btn_sure = view.findViewById(R.id.btn_sure);
        btn_cancel = view.findViewById(R.id.btn_cancel);

        card.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

        lin_parent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popu.dismiss();
            }
        });


        btn_sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onSure();
                }
                popu.dismiss();

            }
        });

        btn_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (lin_remind.getVisibility() == View.VISIBLE) {

                    popu.dismiss();
                } else if (View.GONE == lin_remind.getVisibility()) {

                    lin_remind.setVisibility(View.VISIBLE);
                }

            }
        });

    }

    public void showAnswerPopuPopu(View view) {

        popu.showAtLocation(view, Gravity.CENTER, 0, 0);
    }


    private PayStateUtilsListener listener;

    public void setPayStateUtilsListener(PayStateUtilsListener payStateUtilsListener) {

        this.listener = payStateUtilsListener;

    }

    public void dismiss() {
        popu.dismiss();
    }

    public interface PayStateUtilsListener {

        void onSure();

    }


    /**
     * @param state      支付的结果，失败或成功
     * @param content    需要显示的文本
     * @param btnContent 按钮的文本
     */
    public void setPayState(String state, String content, String btnContent) {


        LogUtil.e(state);
        LogUtil.e(content);
        LogUtil.e(btnContent);

        tv_state.setText(state);
        tv_content.setText(content);
        btn_sure.setText(btnContent);

    }

}
